require 'apr.hash'
require 'moonunit'

local md5 = apr.hash.md5
local sha1 = apr.hash.sha1
local md4 = apr.hash.md4

test_hash = moonunit.TestCase:new()

function test_hash:test_md5()
    assert(md5('') == 'd41d8cd98f00b204e9800998ecf8427e')
    assert(md5('a') == '0cc175b9c0f1b6a831c399e269772661')
    assert(md5('abc') == '900150983cd24fb0d6963f7d28e17f72')
    assert(md5('message digest') == 'f96b697d7cb7938d525a2f31aaf161d0')
    assert(md5('abcdefghijklmnopqrstuvwxyz') == 'c3fcd3d76192e4007dfb496cca67e13b')
    assert(md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') == 'd174ab98d277d9f5a5611c2c9f419d9f')
    assert(md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') == '57edf4a22be3c955ac49da2e2107b67a')
    
    assert(md5('', true) ~= 'd41d8cd98f00b204e9800998ecf8427e');
end

function test_hash:test_sha1()
    assert(sha1('') == 'da39a3ee5e6b4b0d3255bfef95601890afd80709')
    assert(sha1('a') == '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8')
    assert(sha1('abc') == 'a9993e364706816aba3e25717850c26c9cd0d89d')
    assert(sha1('message digest') == 'c12252ceda8be8994d5fa0290a47231c1d16aae3')
    assert(sha1('abcdefghijklmnopqrstuvwxyz') == '32d10c7b8cf96570ca04ce37f2a19d84240d3a89')
    assert(sha1('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') == '761c457bf73b14d27e9e9265c46f4b4dda11f940')
    assert(sha1('12345678901234567890123456789012345678901234567890123456789012345678901234567890') == '50abf5706a150990a08b2c5ea40fa0e585554732')

    assert(sha1('', true) ~= 'da39a3ee5e6b4b0d3255bfef95601890afd80709')
end

function test_hash:test_md4()
    assert(md4('') == '31d6cfe0d16ae931b73c59d7e0c089c0')
    assert(md4('a') == 'bde52cb31de33e46245e05fbdbd6fb24')
    assert(md4('abc') == 'a448017aaf21d8525fc10ae87aa6729d')
    assert(md4('message digest') == 'd9130a8164549fe818874806e1c7014b')
    assert(md4('abcdefghijklmnopqrstuvwxyz') == 'd79e1c308aa5bbcdeea8ed63df412da9')
    assert(md4('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') == '043f8582f241db351ce627e153e7f0e4')
    assert(md4('12345678901234567890123456789012345678901234567890123456789012345678901234567890') == 'e33b4ddc9c38f2199c3e7b164fcc0536')

    assert(md4('', true) ~= '31d6cfe0d16ae931b73c59d7e0c089c0');
end
